package com.looso.sfolder.ui;

import com.looso.sfolder.R;
import com.looso.sfolder.data.UserInfo;
import com.looso.sfolder.net.HttpsInterface;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	
	private EditText mUsernameEditText = null;
	private EditText mPasswordEditText = null;
	private Button mLoginButton = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        mUsernameEditText = (EditText) findViewById(R.id.login_username);        
        mPasswordEditText = (EditText) findViewById(R.id.login_password);
        mLoginButton = (Button) findViewById(R.id.login_login);
        
        mLoginButton.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View v) {
				String username = mUsernameEditText.getText().toString();
				String password = mPasswordEditText.getText().toString();
				
				//1. if username or password equal null
				if(username.equals("")){
					Toast.makeText(LoginActivity.this, getString(R.string.error_login_username_null), Toast.LENGTH_SHORT).show();
					return;
				}
				if(password.equals("")){
					Toast.makeText(LoginActivity.this, getString(R.string.error_login_password_null), Toast.LENGTH_SHORT).show();
					return;
				}
				
				//2. login
				LoginProgressDialog loginProgressDialog = new LoginProgressDialog(LoginActivity.this,username,password);
				loginProgressDialog.setMessage(getString(R.string.login_loading));
				loginProgressDialog.show();
				
				
			}
		});
        
        if(UserInfo.getInstance().getIp().equals("")){
        	startActivity(new Intent(LoginActivity.this, SettingActivity.class));
        }
    }
    
    private static final int ERROR_LOGIN_FAILED = 1;
    private UIHandler mUIHandler = new UIHandler();
    class UIHandler extends Handler{
    	@Override
    	public void handleMessage(Message msg) {
    		switch(msg.what){
    			case ERROR_LOGIN_FAILED:
    				Toast.makeText(LoginActivity.this, getString(R.string.error_login_failed), Toast.LENGTH_SHORT).show();
    				break;
    			default:
    				break;
    		}
    	}
    }
    
    class LoginProgressDialog extends LoadingProgressDialog{

    	private String mUsername = null;
    	private String mPassword = null;
    	
		public LoginProgressDialog(Context context,String username,String password) {
			super(context);
			this.mUsername = username;
			this.mPassword = password;
		}

		@Override
		protected void load() {
			if(HttpsInterface.login(mUsername, mPassword)){
				//1.get file list infos					
//				HttpsInterface.getFileList();
//				try {
//					Thread.sleep(2000);
//				} catch (InterruptedException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}				
				
				//2.then jump to the FileBrowserActivity
				finish();
				startActivity(new Intent(LoginActivity.this, FileBrowserActivity.class));
			}else{
				mUIHandler.sendEmptyMessage(ERROR_LOGIN_FAILED);
			}
			
			dismiss();
		}    	
    }
    
    private int groupId = 0;
    private static final int MENUID_SETTINGS = 0;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	MenuItem settingsMenuItem = menu.add(groupId++, MENUID_SETTINGS, 0, R.string.menu_settings);
    	settingsMenuItem.setIcon(android.R.drawable.ic_menu_manage);
    	return true;
    }    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
		case MENUID_SETTINGS:
			startActivity(new Intent(LoginActivity.this, SettingActivity.class));
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
    }
}

